package com.hns.can.report.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.enumrate.ColorCode;
import com.hns.can.master.service.TendencyChartsDataTools;
import com.hns.can.report.entity.BiLineIdxInfoEntity;
import com.hns.can.report.service.LineAnaService;
import com.hns.can.report.vo.LineAnaLineVo;
import com.hns.frame.mybatis.Pagination;

/**
 * 线路画像控制类
 * 
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/lineAna")
public class LineAnaController extends BaseController {
    /**
     * 线路画像service
     */
    @Autowired
    private LineAnaService lineAnaService;
    @Autowired
    private TendencyChartsDataTools tendencyChartsDataTools;
    
    /**
     * 根据线路id查询线路信息
     * 
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineInfoByLineId")
    @ResponseBody
    public Object getCarInfoByCarId(@RequestParam(value = "lineId") String lineId) throws Exception {
    	BaseLineInfoEntity entity = lineAnaService.getLineAnaLineInfo(lineId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 统计线路车辆 在线车辆
     * 
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineCar")
    @ResponseBody
    public Object getLineCar(@RequestParam(value = "lineId") String lineId) throws Exception {
        int carCnt  = lineAnaService.getLineCar(lineId);
        int onLineCnt  = lineAnaService.getOnlineCar(lineId);
        LineAnaLineVo vo = new LineAnaLineVo();
        vo.setCarCnt(carCnt);
        vo.setOnLineCnt(onLineCnt);
    	return this.getReturnResult(vo);
    }
    
    
    /**
     * 车辆列表
     * 
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getRelatedCarPage")
    @ResponseBody
    public Object getRelatedCarPage(LineAnaLineVo vo) throws Exception {
        Pagination page = lineAnaService.getRelatedCarPage(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 相关驾驶员
     * 
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getRelatedDrvPage")
    @ResponseBody
    public Object getRelatedDrvPage(String lineId) throws Exception {
        Pagination page = lineAnaService.getRelatedDrvPage(lineId);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 能耗—总能耗 百公里能耗
     * 
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineEnerIdxInfo")
    @ResponseBody
    public Object getLineEnerIdxInfo(LineAnaLineVo vo) throws Exception {
	    vo.setIdxTypeArr(new String[]{"050101","050102","050103","050104","050105","050106"});
    	List<BiLineIdxInfoEntity>  list= lineAnaService.getLineIdxInfo(vo);
    	return this.getReturnResult(list);
    	
    }
    
    /**
     * 能耗—趋势
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineEnerTrendInfo")
    @ResponseBody   
    public Object getLineEnerTrendInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050201","050202","050203"});
        List<Map<String,Object>> list = lineAnaService.getLineTrendInfo(vo);
        list = tendencyChartsDataTools.fillMultiTendencyData(list, vo.getDateType());
        return this.getReturnResult(list);
    }
    
    /**
     * 线路里程排名 
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineEnerRankInfo")
    @ResponseBody
    public Object getLineEnerRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050301"});
        List<Map<String,Object>> list = lineAnaService.getLineEnerRankInfo(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 线路百公里能耗排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineEnerHukRankInfo")
    @ResponseBody
    public Object getLineEnerHukRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050401","050402","050403"});
        List<Map<String,Object>> list = lineAnaService.getLineEnerHukRankInfo(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询行为数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineAnaBehIdx")
    @ResponseBody
    public Object getLineAnaBehIdx(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050501","050502","050503","050504","050505","050506","050507","050508","050509","050510","050511","050512","050513","050514","050599"});
        List<Map<String,Object>> list = lineAnaService.getLineTrendInfo(vo);
        list = tendencyChartsDataTools.fillMultiTendencyData(list, vo.getDateType());
        return this.getReturnResult(list);
    }
    
    /**
     * 行为排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineBehRankInfo")
    @ResponseBody
    public Object getLineBehRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050701"});
        List<Map<String,Object>> list = lineAnaService.getLineBehRankInfo(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 不良驾驶行为排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineUnBehRankInfo")
    @ResponseBody
    public Object getLineUnBehRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050601"});
        List<Map<String,Object>> list = lineAnaService.getLineAnaRank(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询故障数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineAnaFaultIdx")
    @ResponseBody
    public Object getLineAnaFaultIdx(LineAnaLineVo vo) throws Exception {
    	 vo.setIdxTypeArr(new String[]{"050801","050802","050803"});
     	List<BiLineIdxInfoEntity>  list= lineAnaService.getLineIdxInfo(vo);
     	return this.getReturnResult(list);
    }
    
    /**
     * 查询故障趋势
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineAnaFaultTrend")
    @ResponseBody
    public Object getLineAnaFaultTrend(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"051101"});
        List<Map<String,Object>> list = lineAnaService.getLineTrendInfo(vo);
        list = tendencyChartsDataTools.fillMultiTendencyData(list, vo.getDateType());
        addColorCodeByType(list, "TREND_VAL1");
        return this.getReturnResult(list);
    }

//	private void addColorCodeByNo(List<Map<String, Object>> list) {
//		for(int i=0;i<list.size();i++){
//        	Map<String, Object> map = list.get(i);
//        	String colorNum = (String)map.get("TREND_VAL2");
//        	String code = ColorCode.getCodeByNum(Integer.parseInt(colorNum));
//        	map.put("colorCode", code);
//        }
//	}
	
	private void addColorCodeByType(List<Map<String, Object>> list, String key) {
		for(int i=0;i<list.size();i++){
        	Map<String, Object> map = list.get(i);
        	String type = (String)map.get(key);
        	String code = ColorCode.getCodeByFault(type);
        	map.put("colorCode", code);
        }
	}
    
    /**
     * 行为排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineFaultRankInfo")
    @ResponseBody
    public Object getLineFaultRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"051001"});
        List<Map<String,Object>> list = lineAnaService.getLineAnaRank(vo);
        addColorCodeByType(list, "RANK_VAL1");
        return this.getReturnResult(list);
    }
    
    /**
     * 车辆故障比例图
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getLineUnFaultRankInfo")
    @ResponseBody
    public Object getLineUnFaultRankInfo(LineAnaLineVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"050901"});
        List<Map<String,Object>> list = lineAnaService.getLineAnaRank(vo);
        addColorCodeByType(list, "RANK_VAL1");
        return this.getReturnResult(list);
    }
}
